package com.github.thinwonton.springboot.showcase.transaction.propagation.service.propagation_requires_new;

import com.github.thinwonton.springboot.showcase.transaction.propagation.constant.OrderConstants;
import com.github.thinwonton.springboot.showcase.transaction.propagation.dao.OrdersDao;
import com.github.thinwonton.springboot.showcase.transaction.propagation.domain.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrdersService {
	@Autowired
	private OrdersDao ordersDao;

	public void updateStatusNoTxThrowException(Long id) {
		Orders orders = ordersDao.selectByPrimaryKey(id);
		orders.setStatus(OrderConstants.ORDER_STATUS_PAID);
		ordersDao.updateByPrimaryKeySelective(orders);
		throw new RuntimeException();
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
	public void updateStatus(Long id) {
		Orders orders = ordersDao.selectByPrimaryKey(id);
		orders.setStatus(OrderConstants.ORDER_STATUS_PAID);
		ordersDao.updateByPrimaryKeySelective(orders);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
	public void updateStatusThrowException(Long id) {
		Orders orders = ordersDao.selectByPrimaryKey(id);
		orders.setStatus(OrderConstants.ORDER_STATUS_PAID);
		ordersDao.updateByPrimaryKeySelective(orders);
		throw new RuntimeException();
	}
}
